from rest_framework.decorators import action
from rest_framework.generics import UpdateAPIView
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from meiduo_admin.serializers.orders import OrderSerializer, OrderDetailSerializer
from meiduo_admin.utils import PageNum
from orders.models import OrderInfo, OrderGoods


class OrderListView(ModelViewSet):

    serializer_class = OrderSerializer

    pagination_class = PageNum

    def get_queryset(self):
        # 获取前端传递的keyword值
        keyword = self.request.query_params.get('keyword')
        # 如果keyword是空字符，则说明要获取所有用户数据
        if keyword is '' or keyword is None:
            return OrderInfo.objects.all()
        else:
            return OrderInfo.objects.filter(user=keyword)

class OrderDetailView(ModelViewSet):

        serializer = OrderDetailSerializer

class OrderStatusView(ModelViewSet):

    serializer_class = OrderSerializer
    queryset = OrderInfo.objects.all()
    pagination_class = PageNum

    @action(methods=['put'],detail=True)
    def status(self,request,pk):
        order = self.get_object()
        status = request.data.get('status')
        order.status = status
        order.save()
        ser = self.get_serializer(order)
        return Response({
            'order_id':order.order_id,
            'status':status})



